<template>
  <div class="tag-config-container">
    <!-- 搜索工作栏 -->
    <ContentWrap class="search-wrapper">
      <div class="search-form-container">
        <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="100px"
          label-position="left"
          class="enhanced-search-form"
        >
          <div class="form-row">
            <el-form-item label="分组名称" prop="name" class="form-item">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入分组名称"
                clearable
                @keyup.enter="handleQuery"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:search" />
                </template>
              </el-input>
            </el-form-item>
          </div>

          <div class="action-buttons">
            <el-button @click="handleQuery" class="search-btn">
              <Icon icon="ep:search" class="btn-icon" /> 搜索
            </el-button>
            <el-button @click="resetQuery" class="reset-btn">
              <Icon icon="ep:refresh" class="btn-icon" /> 重置
            </el-button>
            <el-button
              type="primary"
              @click="openForm('create')"
              v-hasPermi="['crm:module:update']"
              class="add-btn"
            >
              <Icon icon="ep:plus" class="btn-icon" /> 新增
            </el-button>
          </div>
        </el-form>
      </div>
    </ContentWrap>

    <!-- 列表区域 -->
    <div class="table-wrapper">
      <div class="table-header">
        <div class="header-icon">
          <Icon icon="ep:list" />
        </div>
        <div class="header-content">
          <h4 class="table-title">标签分组列表</h4>
          <p class="table-subtitle">共 {{ totals }} 个分组</p>
        </div>
      </div>

      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="listData"
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column label="分组名称" align="center" prop="name" width="200">
            <template #default="scope">
              <div class="name-cell">
                <Icon icon="ep:folder" class="cell-icon" />
                <span class="group-name-text">{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="是否共用" align="center" prop="isCommon" width="120">
            <template #default="scope">
              <div class="common-cell">
                <Icon :icon="scope.row.isCommon == 1 ? 'ep:check' : 'ep:close'" class="cell-icon" />
                <el-text v-if="scope.row.isCommon == 1" type="success" class="common-text"
                  >是</el-text
                >
                <el-text type="danger" v-else class="common-text">否</el-text>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="标签" align="left" prop="name" width="200">
            <template #default="scope">
              <div class="tags-cell">
                <div class="tags-container">
                  <el-tag
                    v-for="tag in scope.row.labelList"
                    :key="tag.id"
                    checked
                    :closable="true"
                    :disable-transitions="false"
                    @close="handleClose(tag.id)"
                    :style="{
                      background: tag.bgColor,
                      color: tag.textColor,
                      marginRight: '8px',
                      marginBottom: '8px',
                      border: 'none',
                      cursor: 'pointer',
                      paddingRight: '9px',
                      borderRadius: '6px',
                      fontSize: '12px',
                      fontWeight: '500'
                    }"
                    @click="openFormTag('update', tag.id)"
                    class="enhanced-tag"
                  >
                    {{ tag.name }}
                  </el-tag>
                  <el-button
                    link
                    type="primary"
                    v-hasPermi="['crm:module:update']"
                    @click="openFormTag('create', scope.row.id)"
                    class="add-tag-btn"
                  >
                    <Icon icon="ep:plus" class="btn-icon" />
                    新增标签
                  </el-button>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="备注" align="center" prop="remark" width="200">
            <template #default="scope">
              <div class="remark-cell">
                <Icon icon="ep:document" class="cell-icon" />
                <span class="remark-text">{{ scope.row.remark }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="240"
          >
            <template #default="scope">
              <div class="time-cell">
                <Icon icon="ep:clock" class="cell-icon" />
                <span class="time-text">{{
                  dateFormatter(scope.row, null, scope.row.createTime)
                }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="200" fixed="right">
            <template #default="scope">
              <div class="action-cell">
                <el-button
                  link
                  type="primary"
                  @click="openForm('update', scope.row.id)"
                  v-hasPermi="['crm:module:update']"
                  class="action-button edit-btn"
                >
                  <Icon icon="ep:edit" class="btn-icon" />
                  编辑
                </el-button>
                <el-button
                  link
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  v-hasPermi="['crm:module:update']"
                  class="action-button delete-btn"
                >
                  <Icon icon="ep:delete" class="btn-icon" />
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <Pagination
            :total="totals"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </div>

    <!-- 表单弹窗：添加/修改 -->
    <TagGroupConfigForm ref="formRef" @success="getList" :moduleId="props.moduleId" />
    <!-- 标签弹窗：添加/修改 -->
    <TagConfigForm ref="formTagRef" @success="getList" :moduleId="props.moduleId" />
  </div>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import * as customerTagApi from '@/api/crm/customerTagConfig/index'
import TagGroupConfigForm from './TagGroupConfigForm.vue'
import TagConfigForm from './TagConfigForm.vue'
import { color } from 'echarts'

defineOptions({ name: 'CustomerTagConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const listData = ref<any[]>([]) // 列表的数据
const totals = ref(0)

const props = defineProps({
  moduleId: {
    type: Number
  }
})

watch(
  () => props.moduleId,
  (newVal) => {
    queryParams.moduleId = newVal
    getList()
  }
)

const queryParams = reactive({
  name: null,
  pageNo: 1,
  pageSize: 10,
  moduleId: props.moduleId
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { list, total } = await customerTagApi.getCustomerTagPage(queryParams)
    listData.value = list
    totals.value = total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const formTagRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await customerTagApi.deletelabel(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/*标签集新增*/
const openFormTag = (type: string, id?: number) => {
  formTagRef.value.open(type, id)
}

const handleClose = (id: string) => {
  ElMessageBox.confirm(
    '删除标签后，相应的线索和客户中的此标签也会被删除，且无法恢复。',
    '确认删除标签？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(async () => {
      const res = await customerTagApi.deleteTag(id)
      if (res) ElMessage.success('删除成功!')
      getList()
    })
    .catch(() => {})
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.tag-config-container {
  padding: 0;
  background: $bg-color;

  @media (max-width: 768px) {
    padding: 0;
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.search-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.search-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.search-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  padding: 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      flex-direction: column;
      gap: 12px;
      margin-bottom: 16px;
    }
  }

  .form-item {
    flex: 1;
    min-width: 260px;
    margin-bottom: 0;

    @media (max-width: 768px) {
      min-width: auto;
      flex: none;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 输入框样式 */
.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 操作按钮区域 */
.action-buttons {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 8px;
  }

  .search-btn,
  .reset-btn,
  .add-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: $border-radius-small;
    font-weight: 500;
    transition: all 0.3s ease;

    .btn-icon {
      font-size: 14px;
    }

    @media (max-width: 768px) {
      width: 100%;
      justify-content: center;
      padding: 10px 16px;
    }

    &:hover {
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
  }

  .search-btn {
    background: $primary-color;
    border-color: $primary-color;
    color: white;

    &:hover {
      background: #1d4ed8;
      border-color: #1d4ed8;
    }
  }

  .reset-btn {
    background: $info-color;
    border-color: $info-color;
    color: white;

    &:hover {
      background: #82848a;
      border-color: #82848a;
    }
  }

  .add-btn {
    background: $success-color;
    border-color: $success-color;
    color: white;

    &:hover {
      background: #5daf34;
      border-color: #5daf34;
    }
  }
}

/* 表格区域样式 */
.table-wrapper {
  background: white;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;
  margin-top: 0;

  @media (max-width: 768px) {
    margin: 16px;
    margin-top: 0;
  }
}

.table-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.table-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.table-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.name-cell,
.common-cell,
.tags-cell,
.remark-cell,
.time-cell,
.action-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.cell-icon {
  font-size: 14px;
  flex-shrink: 0;
  color: $primary-color;
}

.group-name-text {
  color: $text-primary;
  font-weight: 600;
  font-size: 14px;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.common-text {
  font-weight: 600;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  width: 100%;
}

.enhanced-tag {
  transition: all 0.3s ease;
  position: relative;

  &:hover {
    transform: scale(1.05);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  }

  :deep(.el-tag__close) {
    display: none;
    position: absolute;
    right: -8px;
    top: -6px;
    color: #fff;
    background-color: $danger-color;
    border-radius: 50%;
    width: 16px;
    height: 16px;
    font-size: 10px;
    line-height: 16px;
    text-align: center;
  }

  &:hover :deep(.el-tag__close) {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.add-tag-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;
  color: $primary-color;
  border: 1px dashed $primary-border;

  .btn-icon {
    font-size: 12px;
  }

  &:hover {
    background: $primary-light;
    border-color: $primary-color;
    transform: scale(1.05);
  }

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 3px 6px;
  }
}

.remark-text {
  color: $text-secondary;
  font-size: 12px;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  @media (max-width: 768px) {
    max-width: 100px;
    font-size: 11px;
  }
}

.time-text {
  color: $info-color;
  font-weight: 500;
  font-family: 'Courier New', monospace;
  background: rgba(144, 147, 153, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 1px 4px;
  }
}

/* 操作按钮 */
.action-button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }

  .btn-icon {
    font-size: 12px;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
  }

  &.edit-btn {
    &:hover {
      background: rgba(0, 82, 204, 0.1);
      color: $primary-color;
    }
  }

  &.delete-btn {
    &:hover {
      background: rgba(245, 108, 108, 0.1);
      color: $danger-color;
    }
  }
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .tag-config-container {
    background: #1f2937;
  }

  .search-wrapper,
  .table-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-container,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .search-title,
  .table-title {
    color: #f9fafb;
  }

  .group-name-text {
    color: #f9fafb;
  }

  .remark-text {
    color: #d1d5db;
  }

  .time-text {
    background: rgba(156, 163, 175, 0.1);
    color: #9ca3af;
  }

  .add-tag-btn {
    color: #60a5fa;
    border-color: rgba(96, 165, 250, 0.3);

    &:hover {
      background: rgba(96, 165, 250, 0.1);
      border-color: #60a5fa;
    }
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .enhanced-input {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .search-header,
  .table-header {
    padding: 12px 16px;
  }

  .search-form-container {
    padding: 12px;
  }

  .pagination-wrapper {
    padding: 12px 16px;
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .group-name-text {
    font-size: 11px;
  }

  .common-text {
    font-size: 10px;
  }

  .remark-text {
    max-width: 80px;
    font-size: 10px;
  }

  .time-text {
    font-size: 10px;
  }

  .action-button {
    padding: 3px 6px;
    font-size: 10px;
  }

  .add-tag-btn {
    font-size: 10px;
    padding: 2px 4px;
  }

  .enhanced-tag {
    font-size: 10px !important;
  }
}
</style>
